package com.example.javatest.controller;

import com.example.javatest.entity.domain.InfoClass;
import com.example.javatest.entity.domain.InfoSystem;
import com.example.javatest.entity.domain.SupAddclass;
import com.example.javatest.entity.domain.request.ClassInfoRequest;
import com.example.javatest.entity.domain.request.SystemInfoRequest;
import com.example.javatest.service.InfoClassService;
import com.example.javatest.service.InfoSystemService;
import com.example.javatest.service.SupAddclassService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
public class infoController {
    @Resource
    private InfoSystemService infoSystemService;

    @Resource
    private InfoClassService infoClassService;

    @Resource
    private SupAddclassService supAddclassService;


    /**
     * 查询所有 系统信息
     * @return
     */
    @GetMapping("/systemInfo")
    public List<InfoSystem> getInfoSystem(@RequestParam(name = "searchKey") String searchKey){
        if(searchKey==""){
            return infoSystemService.list();
        }else{
            return infoSystemService.listByKey(searchKey);
        }
    }

    /**
     * 查询所有班级信息
     */
    @GetMapping("/allClassInfo")
    public List<InfoClass> getAllClassInfo(){
        return infoClassService.list();
    }

    /**
     * 根据班级ID 查询 班级消息
     * @return
     */
    @PostMapping("/classInfo")
    public List<InfoClass> getClassInfo(@RequestBody ClassInfoRequest classInfoRequest ,HttpServletRequest httpServletRequest){
        if(classInfoRequest==null){
            return null;
        }
        int classId=classInfoRequest.getClassId();
        String searchKey = classInfoRequest.getSearchKey();
        return infoClassService.searchClassInfoByID(classId,searchKey);
    }

    /**
     * 新建系统信息
     * @param systemInfoRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/createSystemInfo")
    public boolean crate(@RequestBody SystemInfoRequest systemInfoRequest, HttpServletRequest httpServletRequest){
        if(systemInfoRequest==null){
            return false;
        }
        String title = systemInfoRequest.getInfoTitle();
        String text = systemInfoRequest.getInfoText();
        if(StringUtils.isAnyBlank(title,text)){
            return false;
        }
        return infoSystemService.create(title,text);
    }

    /**
     * 根据系统信息ID删除对应系统信息
     * @return
     */
    @PostMapping("/deleteSystemInfo")
    public boolean delete(@RequestBody SystemInfoRequest systemInfoRequest,HttpServletRequest httpServletRequest){
        int ID=systemInfoRequest.getID();
        if(!infoSystemService.delete(ID)){
            return false;
        }
        return infoSystemService.removeById(ID);
    }

    /**
     * 新建班级消息
     */
    @PostMapping("/createClassInfo")
    public boolean crateClassInfo(@RequestBody ClassInfoRequest classInfoRequest , HttpServletRequest httpServletRequest){
        if( classInfoRequest == null){
            return false;
        }
        int classId = classInfoRequest.getClassId();
        String master = classInfoRequest.getInfoMaster();
        String title = classInfoRequest.getInfoTitle();
        String text = classInfoRequest.getInfoText();
        if(StringUtils.isAnyBlank(title,text,master)){
            System.out.println(classInfoRequest);
            return false;
        }
        return infoClassService.create(title,text,master,classId);
    }

    /**
     * 管理员通过班级消息
     */
    @PostMapping("/suerClassInfo")
    public boolean suerClassInfo(@RequestBody ClassInfoRequest classInfoRequest,HttpServletRequest httpServletRequest){
        if(classInfoRequest==null){
            return false;
        }
        int infoId=classInfoRequest.getClassInfoId();
        return infoClassService.sure(infoId);
    }

    /**
     * 根据班级信息ID号删除班级信息（逻辑删除）
     */
    @PostMapping("/deleteClassInfoo")
    public boolean deleteClassInfo(@RequestBody ClassInfoRequest classInfoRequest,HttpServletRequest httpServletRequest){
        if(classInfoRequest==null){
            return false;
        }
        int infoId=classInfoRequest.getClassInfoId();
        return infoClassService.unSure(infoId);
    }
    /**
     * 查询所有 用户申请加入班级 消息
     */
    @GetMapping("/searchAddClassInfo")
    public List<SupAddclass> searchAddClassInfo(){
        return supAddclassService.list();
    }
}
